using System;

namespace device
{
    [Serializable]
    public class TubeCalculator
    {
        private double[,] CanShu12 = new double[3, 14]
        {
            {
                2.73, 3.823, 7.26, 6.6, 5.94, 5.28, 4.62, 3.96, 3.3, 2.64,
                1.98, 1.32, 0.66, 0.0
            },
            {
                5.874, 2.73, 3.823, 7.26, 6.6, 5.94, 5.28, 4.62, 3.96, 3.3,
                2.64, 1.98, 1.32, 0.66
            },
            {
                24.3, 22.5, 47.253, 43.144, 39.035, 34.926, 30.817, 26.708, 22.599, 18.49,
                14.381, 10.272, 6.163, 2.054
            }
        };

        private double[,] CanShu13 = new double[3, 15]
        {
            {
                2.94, 4.142, 7.92, 7.26, 6.6, 5.94, 5.28, 4.62, 3.96, 3.3,
                2.64, 1.98, 1.32, 0.66, 0.0
            },
            {
                6.293, 2.94, 4.142, 7.92, 7.26, 6.6, 5.94, 5.28, 4.62, 3.96,
                3.3, 2.64, 1.98, 1.32, 0.66
            },
            {
                26.1, 24.3, 51.362, 47.253, 43.144, 39.035, 34.926, 30.817, 26.708, 22.599,
                18.49, 14.381, 10.272, 6.163, 2.054
            }
        };

        private double[,] CanShu14 = new double[3, 16]
        {
            {
                3.15, 4.461, 8.58, 7.92, 7.26, 6.6, 5.94, 5.28, 4.62, 3.96,
                3.3, 2.64, 1.98, 1.32, 0.66, 0.0
            },
            {
                6.713, 3.15, 4.461, 8.58, 7.92, 7.26, 6.6, 5.94, 5.28, 4.62,
                3.96, 3.3, 2.64, 1.98, 1.32, 0.66
            },
            {
                27.9, 26.1, 55.471, 51.362, 47.253, 43.144, 39.035, 34.926, 30.817, 26.708,
                22.599, 18.49, 14.381, 10.272, 6.163, 2.054
            }
        };

        private double[,] CanShu15 = new double[3, 17]
        {
            {
                3.36, 4.779, 9.24, 8.58, 7.92, 7.26, 6.6, 5.94, 5.28, 4.62,
                3.96, 3.3, 2.64, 1.98, 1.32, 0.66, 0.0
            },
            {
                7.133, 3.36, 4.779, 9.24, 8.58, 7.92, 7.26, 6.6, 5.94, 5.28,
                4.62, 3.96, 3.3, 2.64, 1.98, 1.32, 0.66
            },
            {
                29.7, 27.9, 59.58, 55.471, 51.362, 47.253, 43.144, 39.035, 34.926, 30.817,
                26.708, 22.599, 18.49, 14.381, 10.272, 6.163, 2.054
            }
        };

        private double[,] CanShu16 = new double[3, 18]
        {
            {
                3.57, 5.098, 9.9, 9.24, 8.58, 7.92, 7.26, 6.6, 5.94, 5.28,
                4.62, 3.96, 3.3, 2.64, 1.98, 1.32, 0.66, 0.0
            },
            {
                7.552, 3.57, 5.098, 9.9, 9.24, 8.58, 7.92, 7.26, 6.6, 5.94,
                5.28, 4.62, 3.96, 3.3, 2.64, 1.98, 1.32, 0.66
            },
            {
                31.5, 29.7, 63.689, 59.58, 55.471, 51.362, 47.253, 43.144, 39.035, 34.926,
                30.817, 26.708, 22.599, 18.49, 14.381, 10.272, 6.163, 2.054
            }
        };

        private double[,] CanShu17 = new double[3, 19]
        {
            {
                3.78, 5.417, 10.56, 9.9, 9.24, 8.58, 7.92, 7.26, 6.6, 5.94,
                5.28, 4.62, 3.96, 3.3, 2.64, 1.98, 1.32, 0.66, 0.0
            },
            {
                7.972, 3.78, 5.417, 10.56, 9.9, 9.24, 8.58, 7.92, 7.26, 6.6,
                5.94, 5.28, 4.62, 3.96, 3.3, 2.64, 1.98, 1.32, 0.66
            },
            {
                33.3, 31.5, 67.798, 63.689, 59.58, 55.471, 51.362, 47.253, 43.144, 39.035,
                34.926, 30.817, 26.708, 22.599, 18.49, 14.381, 10.272, 6.163, 2.054
            }
        };

        private double[,] CanShu18 = new double[3, 20]
        {
            {
                3.99, 5.735, 11.22, 10.56, 9.9, 9.24, 8.58, 7.92, 7.26, 6.6,
                5.94, 5.28, 4.62, 3.96, 3.3, 2.64, 1.98, 1.32, 0.66, 0.0
            },
            {
                8.391, 3.99, 5.735, 11.22, 10.56, 9.9, 9.24, 8.58, 7.92, 7.26,
                6.6, 5.94, 5.28, 4.62, 3.96, 3.3, 2.64, 1.98, 1.32, 0.66
            },
            {
                35.1, 33.3, 71.906, 67.798, 63.689, 59.58, 55.471, 51.362, 47.253, 43.144,
                39.035, 34.926, 30.817, 26.708, 22.599, 18.49, 14.381, 10.272, 6.163, 2.054
            }
        };

        private double[,] CanShu19 = new double[3, 21]
        {
            {
                4.2, 6.054, 11.88, 11.22, 10.56, 9.9, 9.24, 8.58, 7.92, 7.26,
                6.6, 5.94, 5.28, 4.62, 3.96, 3.3, 2.64, 1.98, 1.32, 0.66,
                0.0
            },
            {
                8.811, 4.2, 6.054, 11.88, 11.22, 10.56, 9.9, 9.24, 8.58, 7.92,
                7.26, 6.6, 5.94, 5.28, 4.62, 3.96, 3.3, 2.64, 1.98, 1.32,
                0.66
            },
            {
                36.9, 35.1, 76.015, 71.906, 67.798, 63.689, 59.58, 55.471, 51.362, 47.253,
                43.144, 39.035, 34.926, 30.817, 26.708, 22.599, 18.49, 14.381, 10.272, 6.163,
                2.054
            }
        };

        private double[,] Parameter { get; set; }

        private double[] LastTemperature { get; set; }

        private double[] CurrentTemperature { get; set; }

        private int NodeNum { get; set; }

        private bool IsFirstCal { get; set; }

        public TubeCalculator()
        {
            IsFirstCal = true;
        }

        private void ChooseParameter(double vol)
        {
            if (vol <= 25.0)
            {
                Parameter = CanShu13;
                NodeNum = 13;
                LastTemperature = new double[17];
                CurrentTemperature = new double[17];
            }
            if (vol > 25.0 && vol <= 35.0)
            {
                Parameter = CanShu14;
                NodeNum = 14;
                LastTemperature = new double[18];
                CurrentTemperature = new double[18];
            }
            if (vol > 35.0 && vol <= 45.0)
            {
                Parameter = CanShu15;
                NodeNum = 15;
                LastTemperature = new double[19];
                CurrentTemperature = new double[19];
            }
            if (vol > 45.0 && vol <= 65.0)
            {
                Parameter = CanShu16;
                NodeNum = 16;
                LastTemperature = new double[20];
                CurrentTemperature = new double[20];
            }
            if (vol > 65.0 && vol <= 85.0)
            {
                Parameter = CanShu17;
                NodeNum = 17;
                LastTemperature = new double[21];
                CurrentTemperature = new double[21];
            }
            if (vol > 85.0)
            {
                Parameter = CanShu18;
                NodeNum = 18;
                LastTemperature = new double[22];
                CurrentTemperature = new double[22];
            }
        }

        private void ChooseParameterFast(double vol)
        {
            if (vol <= 25.0)
            {
                Parameter = CanShu15;
                NodeNum = 15;
                LastTemperature = new double[19];
                CurrentTemperature = new double[19];
            }
            if (vol > 25.0 && vol <= 35.0)
            {
                Parameter = CanShu16;
                NodeNum = 16;
                LastTemperature = new double[20];
                CurrentTemperature = new double[20];
            }
            if (vol > 35.0 && vol <= 45.0)
            {
                Parameter = CanShu17;
                NodeNum = 17;
                LastTemperature = new double[21];
                CurrentTemperature = new double[21];
            }
            if (vol > 45.0 && vol <= 65.0)
            {
                Parameter = CanShu18;
                NodeNum = 18;
                LastTemperature = new double[22];
                CurrentTemperature = new double[22];
            }
            if (vol > 65.0 && vol <= 85.0)
            {
                Parameter = CanShu19;
                NodeNum = 19;
                LastTemperature = new double[23];
                CurrentTemperature = new double[23];
            }
            if (vol > 85.0)
            {
                Parameter = CanShu19;
                NodeNum = 19;
                LastTemperature = new double[23];
                CurrentTemperature = new double[23];
            }
        }

        public double TubeTemperatureCal(double temp, double vol, bool isFast)
        {
            double num = 0.0;
            if (IsFirstCal)
            {
                if (!isFast)
                {
                    ChooseParameter(vol);
                }
                else
                {
                    ChooseParameterFast(vol);
                }
                for (int i = 0; i < NodeNum + 3; i++)
                {
                    LastTemperature[i] = temp;
                }
                num = temp;
                IsFirstCal = false;
            }
            else
            {
                for (int j = 0; j < 11; j++)
                {
                    for (int k = 0; k < NodeNum + 2; k++)
                    {
                        CurrentTemperature[k + 1] = (Parameter[0, k] * LastTemperature[k + 2] + Parameter[1, k] * LastTemperature[k] + LastTemperature[k + 1] * (Parameter[2, k] - Parameter[0, k] - Parameter[1, k])) / Parameter[2, k];
                    }
                    CurrentTemperature[0] = temp;
                    CurrentTemperature[NodeNum + 3] = CurrentTemperature[NodeNum + 2];
                    for (int l = 0; l < NodeNum + 4; l++)
                    {
                        LastTemperature[l] = CurrentTemperature[l];
                    }
                }
                double num2 = 0.0;
                for (int m = 0; m < NodeNum; m++)
                {
                    num += CurrentTemperature[m + 3] * ((double)(NodeNum - m) - 0.5);
                    num2 += (double)(NodeNum - m) - 0.5;
                }
                num /= num2;
            }
            return num;
        }
    }
}
